package overun.selected;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import overun.enums.GenderEnum;
import overun.service.ValueSetService;
import overun.utils.SpringUtil;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * 基础转换器
 *
 * */
public class BaseConverter implements Converter<String> {


    private ValueSetService valueSetService;

    public BaseConverter() {
        valueSetService = SpringUtil.getBean(ValueSetService.class);
    }

    @Override
    public Class<?> supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return null;
    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取字典类型
        Field field = contentProperty.getField();
        ExcelSelected excelSelected = field.getAnnotation(ExcelSelected.class);
        String keyWord = excelSelected.keyWord();

        // 为空返回
        String dictLabel = cellData.getStringValue();
        if (StringUtils.isBlank(dictLabel)) {
            return dictLabel;
        }

        Map<String, String> valueByKeyWord3 = valueSetService.getValueByKeyWord3(keyWord);

        return valueByKeyWord3.get(dictLabel);
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        // 获取字典类型
        Field field = contentProperty.getField();
        ExcelSelected excelSelected = field.getAnnotation(ExcelSelected.class);
        String keyWord = excelSelected.keyWord();

        // 为空返回
        if (StringUtils.isBlank(value)) {
            return new WriteCellData<>(value);
        }

        Map<String, String> valueByKeyWord2 = valueSetService.getValueByKeyWord2(keyWord);
        String v = valueByKeyWord2.get(value);
        return new WriteCellData<>(v);
    }
}

